package org.alvin.switchserver;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.alvin.switchserver.ui.models.ServerNodeTableModel;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.List;

public class ForwardingHandler extends ChannelInboundHandlerAdapter {

    private ChannelHandlerContext proxyCtx;
    private ServerNodeTableModel serverNodeTableModel ;

    public ForwardingHandler(ChannelHandlerContext ctx,ServerNodeTableModel serverNodeTableModel  ) {
        this.proxyCtx = ctx;
        this.serverNodeTableModel = serverNodeTableModel;
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        // 处理接收到的数据
        for (ServerNode serverNode : this.serverNodeTableModel.getDataList()) {
            if (!serverNode.isDefault()) {
                continue;
            }
            System.out.println("代理回写111:" + ctx.channel().remoteAddress().toString());
            System.out.println("代理回写222:" + serverNode.getChannel().remoteAddress().toString());
            if (ctx.channel().remoteAddress().toString().equalsIgnoreCase(serverNode.getChannel().remoteAddress().toString())) {
                proxyCtx.channel().writeAndFlush(msg);
                break;
            }
        }
    }


}